package com.yxq.mdt.app.modules.market.ui;

import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ScreenUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.market.bean.MallGoodsBean;

import java.util.List;

import q.rorbin.badgeview.DisplayUtil;

/**
 * @author L
 * @time 2019-10-21
 * @describe 商城分类商品数据适配器
 */
public class MainMallGoodsListAdapter extends BaseQuickAdapter<MallGoodsBean.ListBean, BaseViewHolder> {

    private int row = 2;

    public void setRow(int row) {
        this.row = row;
    }

    public int getRow() {
        return row;
    }

    int screenWidth = 0;

    public MainMallGoodsListAdapter(int layoutResId, @Nullable List<MallGoodsBean.ListBean> data) {
        super(layoutResId, data);

    }

    @Override
    protected void convert(BaseViewHolder helper, MallGoodsBean.ListBean item) {
        screenWidth = ScreenUtils.getScreenWidth() - DisplayUtil.dp2px(mContext, (getRow() - 1 + 2) * 15);
        ImageView icon = helper.getView(R.id.main_item_iv_grid_icon);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.width = screenWidth / getRow();
        layoutParams.height = (helper.getLayoutPosition() % 2 == 0) ? layoutParams.width : layoutParams.width + 40;
//        layoutParams.height = layoutParams.width;
        icon.setLayoutParams(layoutParams);

        GlideHelper.setLoadRoundImage(mContext, item.getPic_url(), R.color.gray, icon);

        helper.setText(R.id.main_item_tv_grid_title, item.getName());
        TextView priceView = helper.getView(R.id.main_item_tv_grid_sell_price);
        priceView.setText(String.format("￥%.1f", item.getPrice()));
        helper.setText(R.id.main_item_tv_grid_price, ("已售" + item.getSales()));
    }
}
